package com.iqiyi.pps.epg.web.webapp.action.house;

import com.iqiyi.pps.epg.core.dao.base.Page;
import com.iqiyi.pps.epg.core.dao.base.PropertyFilter;
import com.iqiyi.pps.epg.core.model.house.House;
import com.iqiyi.pps.epg.core.service.house.HouseService;
import com.iqiyi.pps.epg.web.utils.Struts2Utils;
import com.iqiyi.pps.epg.web.webapp.action.BaseAction;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;

import java.util.List;

/**
 * User: rainliang
 * Mail: rainliang@qiyi.com
 * Date: 2014/7/7
 * Time: 15:52
 * Desc:
 */
@Namespace("/house/house")
@Scope("prototype")
@Results({@Result(name = BaseAction.RELOAD, location = "house.action", type = "redirect")})
public class HouseAction extends BaseAction<House>{

    @Autowired
    private HouseService houseService;


    private Page<House> page;
    private House entity;
    private Long id;

    @Override
    @Action(value="list")
    public String list() throws Exception {
        List<PropertyFilter> filters = PropertyFilter.buildFromHttpRequest(Struts2Utils.getRequest());
        //设置默认排序方式
        if (!page.isOrderBySetted()) {
            page.setOrderBy("id");
            page.setOrder(Page.ASC);
        }
        page = houseService.getPageList(page, filters);
        return SUCCESS;
    }

    @Override
    @Action(value="input")
    public String input() throws Exception {
        return SUCCESS;
    }

    @Override
    @Action(value="save")
    public String save() throws Exception {
        if(entity==null){
            addActionMessage("保存失败，id为空");
        }else {
            houseService.save(entity);
            addActionMessage("保存成功");
        }

        return INPUT;
    }

    @Override
    @Action(value="delete")
    public String delete() throws Exception {
        if(id==null){
            code = "A00001";
            message = "id为空";
        }else{
            houseService.delete(id);
            code = "A00000";
            message = "删除成功";
        }
        renderAjaxMessage(code,message);
        return null;

    }

    @Override
    protected void prepareModel() throws Exception {
        if(id!=null){
            entity=houseService.getById(id);
            if(entity==null){
                entity=new House();
            }
        }else{
            entity=new House();
        }
    }

    @Override
    public House getModel() {
        return entity;
    }


    public Page<House> getPage() {
        return page;
    }

    public void setPage(Page<House> page) {
        this.page = page;
    }

    public House getEntity() {
        return entity;
    }

    public void setEntity(House entity) {
        this.entity = entity;
    }

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }
}
